package module.map.nodeList
{
	import drongo.desktop.selector.SelectorEvent;
	import drongo.model.events.ModelEvent;
	import drongo.model.propertys.ArrayProperty;
	import drongo.model.values.CollectionValue;
	
	import flash.display.Stage;
	import flash.events.Event;
	
	import common.view.FGUIMediator;
	
	import fairygui.GButton;
	import fairygui.GObject;
	import fairygui.event.ItemEvent;
	import fairygui.event.StateChangeEvent;
	
	import module.map.model.MapModel;
	import module.map.model.ProjectModel;
	import module.map.model.Propertys;
	
	import ui.Map.UI_ChildrenPanel_item;
	import ui.Map.UI_NodePanel;
	
	/**
	 * 节点列表
	 */
	public class NodeListMediator extends FGUIMediator
	{
		[Inject]
		public var view:UI_NodePanel;
		[Inject]
		public var project:ProjectModel;
		private var stage:Stage;
		
		private var currentMap:MapModel;
		public function NodeListMediator()
		{
			super();
		}
		
		override public function initialize():void{
			super.initialize();
			this.view.m_list.itemRenderer=ListItemRender;
			this.stage=this.view.displayListContainer.stage;
			this.currentMap=this.project.currentMap;
			this.Refresh();
			this.AddEvents();
		}
		
		override public function destroy():void{
			super.destroy();
			this.RemoveEvents();
		}
		
		private function AddEvents():void
		{
			this.project.addEventListener(Event.CHANGE,this.SwitchMapHandler);
			this.view.m_list.addEventListener(ItemEvent.CLICK,ListItemClick);
			this.view.m_listHeader.m_status.addEventListener(StateChangeEvent.CHANGED,this.AllVisibleButtonClick);
			this.view.m_listHeader.m_lock.addEventListener(StateChangeEvent.CHANGED,this.AllLockButtonClick);
			if(this.currentMap){
				this.nodes.addEventListener(ModelEvent.ADD_CHILD,this.NodesChanged);
				this.nodes.addEventListener(ModelEvent.REMOVE_CHILD,this.NodesChanged);
				this.nodes.addEventListener(ModelEvent.CHILD_VALUE_CHANGED,this.NodesChanged);
				this.currentMap.selector.addEventListener(SelectorEvent.SELECTLIST_CHANGE,this.SelectListChanged);
			}
			
			this.stage.addEventListener(Event.ENTER_FRAME,this.Tick);
		}
		
		private function RemoveEvents():void{
			this.project.removeEventListener(Event.CHANGE,this.SwitchMapHandler);
			this.view.m_list.removeEventListener(ItemEvent.CLICK,ListItemClick);
			this.view.m_listHeader.m_status.removeEventListener(StateChangeEvent.CHANGED,this.AllVisibleButtonClick);
			this.view.m_listHeader.m_lock.removeEventListener(StateChangeEvent.CHANGED,this.AllLockButtonClick);
			if(this.currentMap){
				this.nodes.removeEventListener(ModelEvent.ADD_CHILD,this.NodesChanged);
				this.nodes.removeEventListener(ModelEvent.REMOVE_CHILD,this.NodesChanged);
				this.nodes.removeEventListener(ModelEvent.CHILD_VALUE_CHANGED,this.NodesChanged);
				this.currentMap.selector.removeEventListener(SelectorEvent.SELECTLIST_CHANGE,this.SelectListChanged);
			}
			
			this.stage.removeEventListener(Event.ENTER_FRAME,this.Tick);
		}
		
		private function AllLockButtonClick(e:Event):void{
			if(this.nodes==null||this.nodes.length==0){
				return;
			}
			var state:int=this.view.m_listHeader.m_lock.selected?1:0;
			var node:CollectionValue;
			for (var i:int = 0; i < this.nodes.length; i++) 
			{
				node=this.nodes.GetAt(i) as CollectionValue;
				node.Update(Propertys.LOCK,state);
			}
		}
		
		private function AllVisibleButtonClick(e:Event):void{
			if(this.nodes==null||this.nodes.length==0){
				return;
			}
			var state:int=this.view.m_listHeader.m_status.selected?1:0;
			var node:CollectionValue;
			for (var i:int = 0; i < this.nodes.length; i++) 
			{
				node=this.nodes.GetAt(i) as CollectionValue;
				node.Update(Propertys.VISIBLE,state);
			}
		}
		
		private function SwitchMapHandler(e:Event=null):void{
			if(this.currentMap){
				this.nodes.removeEventListener(ModelEvent.ADD_CHILD,this.NodesChanged);
				this.nodes.removeEventListener(ModelEvent.REMOVE_CHILD,this.NodesChanged);
				this.nodes.removeEventListener(ModelEvent.CHILD_VALUE_CHANGED,this.NodesChanged);
				this.currentMap.selector.removeEventListener(SelectorEvent.SELECTLIST_CHANGE,this.SelectListChanged);
			}
			currentMap=this.project.currentMap;
			if(this.currentMap){
				this.nodes.addEventListener(ModelEvent.ADD_CHILD,this.NodesChanged);
				this.nodes.addEventListener(ModelEvent.REMOVE_CHILD,this.NodesChanged);
				this.nodes.addEventListener(ModelEvent.CHILD_VALUE_CHANGED,this.NodesChanged);
				this.currentMap.selector.addEventListener(SelectorEvent.SELECTLIST_CHANGE,this.SelectListChanged);
			}
			this.Refresh();
		}
		
		private function SelectListChanged(e:SelectorEvent):void{
			this.CallNextFrame(this.Refresh);
		}
		
		private function NodesChanged(e:Event):void{
			this.CallNextFrame(this.Refresh);
		}
		
		private function Refresh():void{
			this.view.m_list.removeChildrenToPool();
			if(this.nodes&&this.nodes.length){
				this.view.m_list.numItems=this.nodes.length;
			}
		}
		
		private function ListItemRender(index:int,obj:GObject):void{
			var item:UI_ChildrenPanel_item=obj as UI_ChildrenPanel_item;
			var itemData:CollectionValue=nodes.GetAt(index) as CollectionValue;
			//visible
			item.m_status.selected=itemData.Get(Propertys.VISIBLE).GetValue()==1;
			//lock
			item.m_lock.selected=itemData.Get(Propertys.LOCK).GetValue()==1;	
			//标签
			item.title=itemData.Get(Propertys.NAME).GetValue();
			//如果选中内容中有当前内容
			if(this.currentMap.selector.length>0){
				if(this.currentMap.selector.selectedList.indexOf(itemData)>=0){
					item.selected=true;
				}
			}
			item.data=item.m_status.data=item.m_lock.data=itemData;
			item.m_status.addEventListener(StateChangeEvent.CHANGED,this.VisibleButtonClick);
			item.m_lock.addEventListener(StateChangeEvent.CHANGED,this.LockButtonClick);
		}
		
		private function VisibleButtonClick(e:Event):void{
			var target:GButton=e.target as GButton;
			var node:CollectionValue=target.data as CollectionValue;
			var state:int=target.selected?1:0;
			node.Update(Propertys.VISIBLE,state);
		}
		
		private function LockButtonClick(e:Event):void{
			var target:GButton=e.target as GButton;
			var node:CollectionValue=target.data as CollectionValue;
			var state:int=target.selected?1:0;
			node.Update(Propertys.LOCK,state);
		}
		private var listSelect:Vector.<int>=new Vector.<int>();
		private function ListItemClick(e:ItemEvent):void{
			if(this.currentMap==null){
				return;
			}
			listSelect.length=0;
			this.view.m_list.getSelection(listSelect);
			if(listSelect.length==0){
				return;
			}
			var dataList:Array=[];
			var index:int;
			for (var i:int = 0; i < listSelect.length; i++) 
			{
				index=listSelect[i];
				dataList.push(this.nodes.GetAt(index));
			}
			this.currentMap.selector.resetSelectList(false);
			this.currentMap.selector.push(dataList);
		}
		
		private function get nodes():ArrayProperty{
			if(this.currentMap==null){
				return null;
			}
			return this.currentMap.Get(Propertys.NODES) as ArrayProperty;
		}
	}
}